﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    public partial class StandardReportHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string begindate = Request.QueryString["begindate"] != null ? Request.QueryString["begindate"].ToString() : "";
            string enddate = Request.QueryString["enddate"] != null ? Request.QueryString["enddate"].ToString() : "";
            string sendpoints = Request.QueryString["sendpoints"] != null ? Request.QueryString["sendpoints"].ToString() : "";
            string wthspans = Request.QueryString["wthspans"] != null ? Request.QueryString["wthspans"].ToString() : "";
            string tempspans = Request.QueryString["tempspans"] != null ? Request.QueryString["tempspans"].ToString() : "";
            string tablename = Request.QueryString["tablename"] != null ? Request.QueryString["tablename"].ToString() : ""; ;
            string tid = Request.QueryString["tid"] != null ? Request.QueryString["tid"].ToString() : "";

            if (begindate != "" && enddate != "" && sendpoints != "" && wthspans != "" && tempspans != "" && tablename != "" && tid != "")
                Response.Write(StandardReport(begindate, enddate, sendpoints, wthspans, tempspans, tablename, tid));

        }

        /// <summary>
        /// 核查表数据报表
        /// </summary>
        /// <param name="begindate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="sendpoints">发报时刻数组</param>
        /// <param name="wthspans">天气状态时间段</param>
        /// <param name="tempspans">温度状态时间段</param>
        /// <param name="tablename">数据表类型</param>
        /// <param name="tid">执行任何号</param>
        /// <returns></returns>
        public string StandardReport(string begindate, string enddate, string sendpoints, string wthspans, string tempspans, string tablename, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            DateTime beginDate = DateTime.Parse(begindate);
            DateTime endDate = DateTime.Parse(enddate);
            ForeSemiType semiType = ForeSemiType.Auto;
            DataTableType tableName = (DataTableType)Enum.Parse(typeof(DataTableType), tablename);

            List<ForeSendpointType> sendpointList = new List<ForeSendpointType>();
            foreach (string s in sendpoints.Split(','))
                sendpointList.Add((ForeSendpointType)Enum.Parse(typeof(ForeSendpointType), s.Replace("'", "")));
            ForeSendpointType[] sendpointArray = sendpointList.ToArray();

            List<ForeSpanType> spanList = new List<ForeSpanType>();
            foreach (string s in wthspans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] wthSpanArray = spanList.ToArray();

            spanList.Clear();
            foreach (string s in tempspans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] tempSpanArray = spanList.ToArray();

            ForeSendpointType[] sendPoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };

            StandardReport standardReport = new StandardReport(connectionString, tableName, tid);
            standardReport.PrepareData(beginDate, endDate, semiType, wthSpanArray, tempSpanArray, sendpointArray);
            return standardReport.GetReportDataString();

        }

    }
}